package com.activiti.z_six.service.impl;

import com.activiti.z_six.entity.taskAssignee.AssigneeUserEntity;
import com.activiti.z_six.mapper.taskAssigneeMapper.AssigneeUserMapper;
import com.activiti.z_six.security.RedisUtils;
import com.activiti.z_six.service.AssigneeUserService;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class AssigneeUserServiceImpl implements AssigneeUserService {
    @Autowired
    private AssigneeUserMapper assigneeUserMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public List<AssigneeUserEntity> getAssigneeUserWithTaskKey(String usertaskid){
        List<AssigneeUserEntity> assigneeUserEntityList;
        String assigneeString ="";
        if(redisUtils.exists(usertaskid)){
            assigneeString=redisUtils.get(usertaskid).toString();
        }

        //如果redis中不存在
        if (SystemConfig.IsNullOrEmpty(assigneeString)) {
            assigneeUserEntityList=assigneeUserMapper.getAssigneeUserWithTaskKey(usertaskid);
        }
        else {
            assigneeUserEntityList=JSONObject.parseArray(assigneeString, AssigneeUserEntity.class);
            if(SystemConfig.IsNullOrEmpty(assigneeUserEntityList.get(0).getUsername())){
                assigneeUserEntityList=assigneeUserMapper.getAssigneeUserWithTaskKey(usertaskid);
            }
        }
        return assigneeUserEntityList;
    }
    @Override
    public AjaxResponse setAssigneeUser(String assigneeList,String usertaskid){
        try{
            JSONArray userlist = JSONObject.parseArray(assigneeList);
            List<AssigneeUserEntity> assigneeUserEntityList = new ArrayList<>();
            for (int i = 0; i < userlist.size(); i++) {
                JSONObject json = JSONObject.parseObject(JSON.toJSONString(userlist.get(i)));
                AssigneeUserEntity assigneeUserEntity = new AssigneeUserEntity();
                if(SystemConfig.IsNullOrEmpty(json.getString("username"))){
                    assigneeUserEntity.setUsername(json.getString("id"));
                }
                else {
                    assigneeUserEntity.setUsername(json.getString("username"));
                }
                assigneeUserEntity.setUsertaskid(usertaskid);
                assigneeUserEntity.setId(UUID.randomUUID().toString());
                assigneeUserEntityList.add(assigneeUserEntity);
            }
            assigneeUserMapper.setAssigneeUser(assigneeUserEntityList);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "执行成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "执行失败", ex.toString());
        }
    }
    @Override
    public AjaxResponse deleteAssigneeUser(String usertaskid,String username){
        try{
            AssigneeUserEntity assigneeUserEntity=new AssigneeUserEntity();
            assigneeUserEntity.setUsertaskid(usertaskid);
            assigneeUserEntity.setUsername(username);
            assigneeUserMapper.deleteAssigneeUser(assigneeUserEntity);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "执行成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "执行失败", ex.toString());
        }
    }
    @Override
    public AjaxResponse deleteAssigneeUserByTaskid(String usertaskid){
        try{
            assigneeUserMapper.deleteAssigneeUserByTaskid(usertaskid);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "执行成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "执行失败", ex.toString());
        }
    }
}
